package com.tgy.MyFlowable.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.core.common.enums.StatusType;
import com.ctsi.core.common.util.$;
import com.ctsi.core.common.util.StringPool;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.workflow.dto.FlwShortcutDTO;
import com.ctsi.workflow.entity.FlwShortcut;
import com.ctsi.workflow.mapper.FlwShortcutMapper;
import com.ctsi.workflow.service.IFlwShortcutService;
import com.ctsi.workflow.vo.FlwShortcutTreeNode;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 流程申请入口表 服务实现类
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/8 19:48
 */
@Service
public class FlwShortcutServiceImpl extends ServiceImpl<FlwShortcutMapper, FlwShortcut> implements IFlwShortcutService {

    @Override
    public IPage<FlwShortcut> listPage(FlwShortcutDTO flwShortcutDTO) {
        LambdaQueryWrapper<FlwShortcut> queryWrapper = Wrappers.lambdaQuery();
        // 模糊查询名称
        queryWrapper.like($.isNotBlank(flwShortcutDTO.getName()), FlwShortcut::getName,
                StringPool.PERCENT + flwShortcutDTO.getName() + StringPool.PERCENT);
        // 查询目录
        queryWrapper.eq($.isNotBlank(flwShortcutDTO.getCategory()), FlwShortcut::getCategory, flwShortcutDTO.getCategory());
        queryWrapper.orderByDesc(FlwShortcut::getCreateTime);
        return this.baseMapper.selectPage(PageUtil.getPage(flwShortcutDTO), queryWrapper);
    }

    @Override
    public void changeStatus(String id, Integer status) {
        FlwShortcut flwShortcut = this.getOne(Wrappers.<FlwShortcut>lambdaQuery().eq(FlwShortcut::getProcessDefinitionId, id));
        if ($.isNotEmpty(flwShortcut)) {
            flwShortcut.setStatus(status);
            this.updateById(flwShortcut);
        }
    }

    @Override
    public void delete(String id) {
        FlwShortcut one = this.getOne(Wrappers.<FlwShortcut>lambdaQuery().eq(FlwShortcut::getProcessDefinitionId, id));
        if ($.isNotEmpty(one)) {
            // 只是修改状态为2，非物理删除
            one.setStatus(2);
            this.updateById(one);
        }
    }

    @Override
    public List<FlwShortcutTreeNode> list(FlwShortcut flwShortcut) {
        QueryWrapper<FlwShortcutTreeNode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b.status", StatusType.ENABLE.getCode());
        //根据排序升序排列，序号越小越在前
        queryWrapper.orderByAsc("b.sort");
        return this.baseMapper.list(queryWrapper);
    }
}
